import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    len:0,
    content:[],
    info:[],
    cleanContext:[],
    search:undefined,
  },
  mutations: {

    setSearch(state,value){
      state.search=value
    },
     setAll(){
      const modulesFiles = require.context('../assets', true, /.md$/)
      const modules = modulesFiles.keys().reduce((modules, modulePath) => {
        const moduleName = modulePath.replace(/^.\/(.*)\.md/,'$1')
        const value = modulesFiles(modulePath)
        modules[moduleName] = value.default
        return modules
      }, {})
      this.state.len = Object.keys(modules).length
      Object.keys(modules).forEach((name)=>{
        var tmp = require('../assets/'+name+'.md')

        this.state.content.push(tmp)
      })

      this.state.content.forEach((con)=>{
        var start = false
        var end =false
        var info = []
        var content = ""

        con.trim().split('\n').forEach((n)=>{

          if(n.trim().toString() === '---' && start===false){
            start = true
          }
          else if(n.trim().toString() ==='---' && end ===false && start ===true){
            end = true
          }
          else if(start === true && end === false) {
            info.push(n)
          }
          else if(start ===true && end ===true){
            content=content+n+"\n";
          }

        })
        this.state.cleanContext.push(content)

        var key=[]
        var value=[]
        info.forEach((n)=>{
          var temp = n.split(':')
          key.push(temp[0].trim().toString())
          value.push(temp[1].trim().toString())
        })

        const getnewarr = (keyArr,valueArr)=>
        {
          var obj = {};
          keyArr.map((v, i) => {
            obj[keyArr[i]] = valueArr[i];
          })
          return obj;
        }
        this.state.info.push(getnewarr(key,value))
      })

    }
  },
  actions: {
  },
  modules: {
  }
})
